home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Freeware / Miro 1.0 / Miro_Installer.exe / xulrunner / python / frontend_implementation / urlcallbacks.py < prev    next >
Encoding:
Python Source  |  2007-11-12  |  3.1 KB  |  93 lines

  1. # Miro - an RSS based video player application
  2. # Copyright (C) 2005-2007 Participatory Culture Foundation
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 2 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
  17.  
  18. """Handles installing callbacks for url loads of HTMLDisplay objects.
  19.  
  20. The way this module is used is HTMLDisplay installs a bunch of callbacks with
  21. installCallback(), then the mycontentpolicy XPCOM object calls runCallback()
  22. when it sees a new URL.  This means that installCallback is called from the
  23. backend event loop, while runCallback is called from the frontend event loop.
  24. """
  25.  
  26. import config
  27. import util
  28. import prefs
  29. from threading import Lock
  30.  
  31. callbacks = {}
  32. mainDisplayCallback = None
  33. # We assume all urls that don't begin with file:// go to the mainDisplay
  34. # FIXME: we may want a cleaner way to do this.
  35. callbacksLock = Lock()
  36.  
  37. def installCallback(referrer, callback):
  38.     """Install a new URL load callback, callback will be called when we see a
  39.     URL load where the referrer matches referrer.
  40.     Callback should accept a URL as an input and return True if we should
  41.     continue the load, or False if the we shouldn't.
  42.     """
  43.     callbacksLock.acquire()
  44.     try:
  45.         callbacks[referrer] = callback
  46.     finally:
  47.         callbacksLock.release()
  48.  
  49. def installMainDisplayCallback(callback):
  50.     """Install a callback for urls where the referrerer is any channel guide
  51.     page.  """
  52.     global mainDisplayCallback
  53.     callbacksLock.acquire()
  54.     try:
  55.         mainDisplayCallback = callback
  56.     finally:
  57.         callbacksLock.release()
  58.  
  59. def removeCallback(referrer):
  60.     """Remove a callback created with installCallback().  If a callback
  61.     doesn't exist for referrer, a KeyError will be thrown.
  62.     """
  63.     callbacksLock.acquire()
  64.     try:
  65.         del callbacks[referrer]
  66.     finally:
  67.         callbacksLock.release()
  68.  
  69. def runCallback(referrerURL, url):
  70.     """Try to find an installed callback and run it if there is one.  If this
  71.     method return True, the URL load should continue, if it returns False it
  72.     should stop.
  73.     """
  74.  
  75.     callbacksLock.acquire()
  76.     try:
  77.         try:
  78.             callback = callbacks[referrerURL]
  79.         except KeyError:
  80.             if (not url.startswith("file://") and 
  81.                     mainDisplayCallback is not None):
  82.                 callback = mainDisplayCallback
  83.             else:
  84.                 return True
  85.     finally:
  86.         callbacksLock.release()
  87.     try:
  88.         rv = callback(url)
  89.         return rv
  90.     except:
  91.         util.failedExn(when="When running URL callback")
  92.         return True
  93.